package com.aloda.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.annotation.AdminAuthCheck;
import com.aloda.annotation.LoginLogAnno;
import com.aloda.annotation.OperateLogAnno;
import com.aloda.context.AdminBaseContext;
import com.aloda.dto.emp.*;
import com.aloda.entity.Emp;
import com.aloda.query.EmpPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.EmpService;
import com.aloda.utils.EncryptionUtil;
import com.aloda.vo.emp.EmpInfoVO;
import com.aloda.vo.emp.EmpLoginVO;
import com.aloda.vo.emp.EmpPageVO;
import com.aloda.vo.emp.EmpSafetyVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工controller层
 * @author aloda
 * @date 2024/12/16
 */
@Slf4j
@RequestMapping("/admin/emp")
@RestController
public class EmpController {

    @Resource
    private EmpService empService;

    /**
     * 通过账号密码登录
     * @param empLoginDTO
     * @return
     */
    @LoginLogAnno(desc = "员工登录")
    @PostMapping("/login")
    public Result<EmpLoginVO> loginByUnAndPw(@RequestBody EmpLoginDTO empLoginDTO) throws Exception {
        log.info("员工登录：{}",empLoginDTO);
        String decrypt = EncryptionUtil.decrypt(empLoginDTO.getPassword());
        empLoginDTO.setPassword(decrypt);
        EmpLoginVO empLoginVO = empService.loginByUnAndPw(empLoginDTO);
        return Result.success(empLoginVO);
    }

    /**
     * 分页查询
     * @param empPageDTO
     * @return
     */
    @AdminAuthCheck
    @PostMapping("/page")
    public Result<PageResult<EmpPageVO>> pageQuery(@RequestBody EmpPageDTO empPageDTO) {
        PageResult<EmpPageVO> pageResult = empService.pageQuery(empPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过id查询员工
     * @param id
     * @return
     */
    @AdminAuthCheck
    @GetMapping("/{id}")
    public Result<EmpSafetyVO> getEmpById(@PathVariable Long id) {
        Emp emp = empService.getById(id);
        return Result.success(BeanUtil.copyProperties(emp, EmpSafetyVO.class));
    }

    /**
     * 新增员工
     * @param empAddDTO
     * @return
     */
    @OperateLogAnno(category = "新增员工")
    @AdminAuthCheck
    @PostMapping
    public Result addEmp(@RequestBody EmpAddDTO empAddDTO) {
        return Result.success(empService.addEmp(empAddDTO));
    }

    /**
     * 编辑员工
     * @param empEditDTO
     * @return
     */
    @OperateLogAnno(category = "编辑员工")
    @AdminAuthCheck
    @PutMapping
    public Result editEmp(@RequestBody EmpEditDTO empEditDTO) {
        return Result.success(empService.editEmp(empEditDTO));
    }

    /**
     * 删除员工
     * @param ids
     * @return
     */
    @OperateLogAnno(category = "删除员工")
    @AdminAuthCheck
    @DeleteMapping
    public Result deleteEmp(@RequestBody List<Long> ids) {
        return Result.success(empService.removeBatchByIds(ids));
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @OperateLogAnno(category = "重置密码")
    @AdminAuthCheck
    @PutMapping("/password/{id}")
    public Result resetPassword(@PathVariable Long id) {
        empService.resetPassword(id);
        return Result.success();
    }

    /**
     * 通过token获取员工个人信息
     * @return
     */
    @GetMapping("/info")
    public Result<EmpInfoVO> getEmpInfoByToken() {
        Long currentId = AdminBaseContext.getCurrentId();
        Emp emp = empService.getById(currentId);
        EmpInfoVO empInfoVO = BeanUtil.copyProperties(emp, EmpInfoVO.class);
        return Result.success(empInfoVO);
    }

    /**
     * 员工编辑个人信息
     * @param empInfoDTO
     * @return
     */
    @OperateLogAnno(category = "员工编辑个人信息")
    @PostMapping("/edit")
    public Result empEditInfo(@RequestBody EmpInfoDTO empInfoDTO) {
        log.info("员工编辑个人信息：{}",empInfoDTO);
        empService.empEditInfo(empInfoDTO);
        return Result.success();
    }

    /**
     * 员工修改个人密码
     * @param empEditPwDTO
     * @return
     */
    @PostMapping("/pw")
    public Result empEditPassword(@RequestBody EmpEditPwDTO empEditPwDTO) {
        empService.empEditPassword(empEditPwDTO);
        return Result.success();
    }

    /**
     * 导出表格
     * @param response
     */
    @OperateLogAnno(category = "导出员工表格信息")
    @AdminAuthCheck
    @PostMapping("/export")
    public void exportEmpToExcel(HttpServletResponse response,  @RequestBody List<Long> ids) {
        empService.exportEmpToExcel(response, ids);
    }
}
